package com.wfm.niuke;

import org.junit.Test;

public class P移除一个数后乘积最大 {
    int maxAfterRemoveOne(int[] nums){
        int n = nums.length;
        if(n<=1) return 0;

        int negativeNum = 0;
        int minNegative = Integer.MAX_VALUE;
        int minNegativeIdx = -1;
        int maxNegative = Integer.MIN_VALUE;
        int maxNegativeIdx = -1;
        int minPositive = Integer.MAX_VALUE;
        int minPositiveIdx = -1;
        for(int i = 0 ;i<nums.length;i++){
            int num = nums[i];
            if (num<0){
                negativeNum++;
                if(minNegative>num){
                    minNegative = num;
                    minNegativeIdx = i;
                }
                if(maxNegative<num){
                    maxNegative = num;
                    maxNegativeIdx = i;
                }
            }else {
                if(minPositive>num){
                    minPositive = num;
                    minPositiveIdx = i;
                }
            }
        }


        // 全是正数
        if(negativeNum==0){
            return minPositiveIdx;
        }
        // 如果负数个数是奇数个，删除负数中最大的
        else if(negativeNum%2==1){
            return maxNegativeIdx;
        }else{ // 如果负数个数是偶数个，删除正数中最小的,如果没有正数，删除负数中最小的
            // 没有正数
            if(minPositive==Integer.MAX_VALUE){
                return minNegativeIdx;
            }else {
                return minPositiveIdx;
            }
        }

    }
    @Test
    public void test(){
        int v = maxAfterRemoveOne(new int[]{-4, 0, 5, 2});//0
        System.out.println(v);
        int v1 = maxAfterRemoveOne(new int[]{-4, 0, -2, 2});//1
        System.out.println(v1);
        int v2 = maxAfterRemoveOne(new int[]{-4,  -2, -2});//1
        System.out.println(v2);
        int v3 = maxAfterRemoveOne(new int[]{-4,  -2, -2,-1});//0
        System.out.println(v3);
    }
    @Test
    public void test2(){
        System.out.println(Math.min(-1, Float.MAX_VALUE));
        System.out.println(Math.max(-1, Float.MIN_VALUE));
        System.out.println();
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Float.MAX_VALUE);
        System.out.println(Float.MIN_VALUE>0);
    }
}
